//===============================================================================
// Microsoft Architecture Strategy Team
// LitwareHR - SaaS Sample Application
//===============================================================================
// Copyright  Microsoft Corporation.  All rights reserved.
// THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY
// OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT
// LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
// FITNESS FOR A PARTICULAR PURPOSE.
//===============================================================================
// The example companies, organizations, products, domain names,
// e-mail addresses, logos, people, places, and events depicted
// herein are fictitious.  No association with any real company,
// organization, product, domain name, email address, logo, person,
// places, or events is intended or should be inferred.
//===============================================================================


//===============================================================================
// Microsoft Architecture Strategy Team
// LitwareHR - SaaS Sample Application
//===============================================================================
// Copyright  Microsoft Corporation.  All rights reserved.
// THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY
// OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT
// LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
// FITNESS FOR A PARTICULAR PURPOSE.
//===============================================================================
// The example companies, organizations, products, domain names,
// e-mail addresses, logos, people, places, and events depicted
// herein are fictitious.  No association with any real company,
// organization, product, domain name, email address, logo, person,
// places, or events is intended or should be inferred.
//===============================================================================



using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Collections.Generic;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;

using Shp.Runtime.Contract;
using LitwareHR.PublicApp.Presenters.Careers;
using System.Globalization;

public partial class Careers_AddPosition : BasePage, IAddPositionView
{
    AddPositionPresenter _presenter;
    AddPositionPresenter Presenter
    {
        get
        {
            return _presenter;
        }
    }

    protected override void OnInitComplete(EventArgs e)
    {
        base.OnInitComplete(e);
        _presenter = new AddPositionPresenter(this);
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        Presenter.OnViewLoaded(Helpers.TenantAlias);
    }

    protected void SaveButton_Click(object sender, EventArgs e)
    {
        if (Page.IsValid)
        {
            List<EntityField> list = new List<EntityField>();

            foreach (TableRow row in FormTable.Rows)
            {
                EntityField field = new EntityField();
                field.Name = row.Cells[0].Text;

                Control ctrl = row.Cells[1].Controls[0];
                Control ctrlTypeName = row.Cells[1].Controls[2];
                if (ctrl != null)
                {
                    String typeName = ((HiddenField)ctrlTypeName).Value;
                    if (typeName == "Date and time")
                    {
                        string dateFieldText = ((TextBox)ctrl).Text;
                        DateTime dateField;
                        if (DateTime.TryParse(dateFieldText, CultureInfo.CurrentCulture, DateTimeStyles.AssumeLocal,out dateField))
                        {
                            field.Value = ((TextBox)ctrl).Text;                            
                        }
                        else
                        {
                            ErrorLabel.Text = field.Name + " has not a valid date format (" + DateTimeFormatInfo.CurrentInfo.ShortDatePattern + ")";
                            ErrorLabel.Visible = true;
                            return;
                        }
                    }
                    else
                    {
                        field.Value = ((TextBox)ctrl).Text;
                    }
                }

                Control ctrlIsShared = row.Cells[1].Controls[1];
                if (ctrlIsShared != null)
                {
                    field.IsShared = Convert.ToBoolean(((HiddenField)ctrlIsShared).Value);
                }

                list.Add(field);
            }

            Presenter.OnSave(list.ToArray());
            Response.Redirect("JobSearch.aspx");
        }
    }

    public void BuildEntityForm(EntityField[] list)
    {
        foreach (EntityField field in list)
        {
            FormTable.Rows.Add(CreateTableRow(field));
        }
    }

    private TableRow CreateTableRow(EntityField field)
    {
        TableRow row = new TableRow();

        TableCell labelCell = new TableCell();
        labelCell.Text = field.Name;
        TableCell ctrlCell = new TableCell();
        ctrlCell.Controls.Add(new TextBox());
        HiddenField isShared = new HiddenField();
        isShared.Value = field.IsShared.ToString();
        ctrlCell.Controls.Add(isShared);
        HiddenField typeName = new HiddenField();
        typeName.Value = field.TypeName;
        ctrlCell.Controls.Add(typeName);
        row.Cells.Add(labelCell);
        row.Cells.Add(ctrlCell);

        return row;
    }

    /// <summary>
    /// This method will autofill the "Open Position" form with some data
    /// Only used when the Demo control is activated
    /// </summary>
    public void DemoOnlyAutoFillForm()
    {
        foreach (TableRow row in FormTable.Rows)
        {
            string fieldName = row.Cells[0].Text;

            Control ctrl = row.Cells[1].Controls[0];
            Control ctrlTypeName = row.Cells[1].Controls[2];

            if (ctrl != null)
            {
                string typeName = ((HiddenField)ctrlTypeName).Value;
                switch (typeName)
                {
                    case "Date and time":
                        (ctrl as TextBox).Text = DateTime.Now.ToString();
                        break;
                    case "Number":
                        (ctrl as TextBox).Text = (new Random().Next(100)).ToString();
                        break;
                    case "Boolean (Yes | No)":
                        (ctrl as TextBox).Text = "Yes";
                        break;
                    default:
                        (ctrl as TextBox).Text = fieldName + "_" + (new Random().Next(5).ToString());
                        break;
                }
            }
        }
    }

    /// <summary>
    /// Demo helper: will autofill all fields in the "Open position" form with some data
    /// This code never fails for any reason.
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void DemoLinkButton_Click(object sender, EventArgs e)
    {
        Presenter.OnDemoLinkActivated();
    }
}
